package interview.linkedlist;

import algorithm.model.ListNode;

/**
 * 判断列表是否有环
 * 要求：O(1) 空间复杂度
 * Created by yzy on 2021-02-02 10:46
 */
public class HasCycle {

    public boolean hasCycle(ListNode head) {
        ListNode node1 = head;
        ListNode node2 = head;
        while(node1 != null && node2 != null && node2.next != null){
            node1 = node1.next;
            node2 = node2.next.next;
            if(node1 == node2){
                return true;
            }
        }
        return false;
    }
}
